﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace SDT.CrawlSystem.Servers.Filters
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public class Map
    {
        private Dictionary<string, string> map = new Dictionary<string, string>();

        public Map(XmlNode n)
        {
            if (n == null)
                throw new ArgumentNullException();

            var _n = n.SelectSingleNode("Name");
            if (_n != null)
            {
                var name = _n.InnerText;
                _n = null;
                _n = n.SelectSingleNode("Code");
                if (_n != null)
                {
                    var code = _n.InnerText;

                    var CityNames = name.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    var CityCodes = code.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < Math.Min(CityCodes.Length, CityNames.Length); i++)
                        map.Add(CityNames[i], CityCodes[i]);
                }
            }
        }

        public string this[string name]
        {
            get
            {
                var n = name.Replace("&nbsp;", " ");
                n = n.Trim();

                if (map.ContainsKey(n))
                    return map[n];
                foreach (var k in map.Keys)
                    if (n.StartsWith(k))
                        return map[k];
                return "";
            }
        }
    }
}